define(function (require) {
    var Base = require('common/base/main');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();
    //日期控件
    require('jqui_extend_slideraccess');
    require('jqui_extend_timepicker');
    require('jqui_lang_datepicker');
    require('jqui_datepicker');
    require('uploadifive');
    var uploadItemTpl = require('text!group/common/templates/upload_tpl.html');

    return Base.extend({
        initialize: function (params) {
            var _this = this;
            Base.prototype.initialize.apply(this, [params]);
            _this.initCateOverlay();
            _this.initCommon();

        },
        initCommon: function () {
            this._bindAddCart();
            this.create_datepicker();
        },
        //添加到购物车
        _bindAddCart: function () {
            var _this = this;
            $('.J_add_cart').unbind('click').bind('click', function () {
                var id = $(this).data('id');
                _this.api({
                    m: 'cart',
                    a: 'add',
                    items: [{item_id: id, num: '1'}]
                }, function (resp) {
                    mask.success(resp.info);
                });
            });
        },
        //导航菜单
        initCateOverlay: function () {
            if (_global.m == 'index') {
                function _hide($this) {
                    $('.J_overlay .right', $this).hide();
                    $('.J_overlay', $this).width($('.J_overlay .left', $this).width());
                }

                _hide($('.J_all_cate'));
                $('.J_overlay').show();
                $('.J_all_cate').hover(function () {
                    $('.J_overlay .right', $(this)).show();
                    $('.J_overlay', $(this)).width(702);
                }, function () {
                    _hide($(this));
                });
            }
            else {
                $('.J_all_cate').hover(function () {
                    $('.J_overlay', $(this)).show();
                }, function () {
                    $('.J_overlay', $(this)).hide();
                });
            }
            $('.J_all_cate .J_overlay .left a').mouseover(function () {
                var id = $(this).data('id');
                $(this).addClass('hover').siblings().removeClass('hover');
                $('.J_sub_cate[data-id=' + id + ']').show().siblings().hide();
            });
        },
        /*
         api请求封装方法
         */
        api: function (params, callback) {
            var api, data;
            if (_.empty(params.m) || _.empty(params.a)) {
                alert("/home/common/main方法api()参数m、a必传!");
                return;
            }
            api = _.sprintf('/api/%s/%s', params.m, params.a);
            data = _.json_encode(_.omit(params, 'm', 'a'));
            utils.ajaxPost({api: api, data: data}, callback);
        },
		/*
         创建日期组件
         */
        create_datepicker: function() {
            var DATE_PICKER_OPT = {
                showWeek: true,
                changeMonth: true,
                changeYear: true,
                dateFormat: 'yy-mm-dd',
                showButtonPanel: true,
                timeFormat: 'HH:mm:ss',
                yearRange: '-100:+10',
                stepHour: 1,
                stepMinute: 1,
                stepSecond: 1,
                timeOnlyTitle: '1',
                timeText: '时间',
                hourText: '小时',
                minuteText: '分钟',
                secondText: '秒',
                currentText: '现在',
                closeText: '关闭'
            };
            if (_.empty($.fn.datepicker)) {
                console.log("$.fn.datepicker为空！");
            }
            $(".J_date_picker").each(function() {
                var opt = DATE_PICKER_OPT,
                    is_short = $(this).hasClass('short');

                if ($(this).attr("data-minDate")) {
                    opt.onClose = function(selectedDate) {
                        if (is_short) {
                            $($(this).attr("data-minDate")).datepicker("option", "minDate", selectedDate);
                        } else {
                            $($(this).attr("data-minDate")).datetimepicker("option", "minDate", selectedDate);
                        }
                    };
                }
                if ($(this).attr("data-maxDate")) {
                    opt.onClose = function(selectedDate) {
                        if (is_short) {
                            $($(this).attr("data-maxDate")).datepicker("option", "maxDate", selectedDate);
                        } else {
                            $($(this).attr("data-maxDate")).datetimepicker("option", "maxDate", selectedDate);
                        }
                    };
                }
                if (is_short) {
                    $(this).datepicker(opt);
                } else {
                    $(this).datetimepicker(opt);
                }
            });
        },
		initUpload: function (params) {
            var _this = this;

            var img_index = 0;
            $('input[type="file"]').each(function () {
                var $this = $(this);
                var id = $this.attr('id');

                var initParams = {
                    buttonText: '',
                    multi: false,
                    width: 150,
                    dnd: false,
                    fileObjName: id,
                    itemTemplate: _this.render(uploadItemTpl, {}),
                    fileType: ['image/jpeg', 'image/jpg', 'image/png', 'image/bmp'],
                    uploadScript: _.wap_url({g:'mall',m: 'assets', a: 'uploadImage'}),
                    onError: function () {

                    },
                    onSelect: function () {

                    },
                    onUploadComplete: function (file, data) {
                        data = JSON.parse(data);
                        if (data.status == 0) {
                            mask.error(data.msg);
                        }
                        else {
                            file.queueItem.css({
                                'background-image': 'url(' + data.data.fullPath + ')'
                            });
                            $('.uploadifive-hidden', file.queueItem).val(data.data.savePath);

                            $('.uploadifive-hidden', file.queueItem).attr('name', id + (initParams.queueSizeLimit == 1 ? '' : '[' + img_index + ']'));
                            img_index++;
                        }
                        if (params._onUploadComplete) {
                            params._onUploadComplete(file, data);
                        }
                    }
                };
                $this.uploadifive(_.extend(initParams, params));
            });
        },
		bind_submit: function (title, fn) {
            var _this = this;
            if (_.empty(title)) {
                title = "操作成功";
            }
            _this.$('form').validate({
                submitHandler: function (form) {
                    var params = _this.$('form').serializeJson();
                    utils.ajaxPost(_.extend(params), function (data) {
                        mask.success(title, function () {
                            if (fn) {
                                fn();
                            }
                            else {
                                window.history.go(-1);
                            }
                        });
                    });
                }
            });
        },
    });
});